/*
 * 文 件 名 : PlanListAdapter.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2013-2-16
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui.plan;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fire8.dolphin.model.DateModel;
import com.fire8.dolphin.model.Plan;
import com.fire8.dolphin.ui.R;
import com.fire8.dolphin.utils.DateUtil;
import com.fire8.dolphin.utils.LogUtil;

/**
 * 计划列表数据适配器
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2013-2-16]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class PlanListAdapter extends BaseAdapter
{
    private HashMap<Integer, View> viewMap = new HashMap<Integer, View>();
    private List<DateModel> dateList;
    private List<Plan> planList;
    private LayoutInflater mInflater;
    private Context context;
    final String dayNames[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    private HashMap<Integer,LinkedHashMap<String, View>> itemViewMap = new HashMap<Integer, LinkedHashMap<String,View>>();
    
    private OnSubItemClickListener subItemClickListener;
    public PlanListAdapter(Context ct,List<DateModel> models,List<Plan> plans)
    {
        context = ct;
        mInflater = LayoutInflater.from(context);
        dateList = models;
        planList = plans;
    }
    
    @Override
    public int getCount()
    {
        return dateList.size();
    }
    
    @Override
    public Object getItem(int position)
    {
        return dateList.get(position);
    }
    
    @Override
    public long getItemId(int position)
    {
        return 0;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        //LogUtil.d(position + "");
        DateModel dm = dateList.get(position);
        //LogUtil.d(dm.getCurrYear() + "");
        View rowView = viewMap.get(position);
        ViewHolder holder = null;
        if (rowView == null)
        {
            holder = new ViewHolder();
            rowView = mInflater.inflate(R.layout.plan_list_item, null);
            holder.tvDateRo = (TextView)rowView.findViewById(R.id.tv_date_ro);
            holder.tvDayRo = (TextView)rowView.findViewById(R.id.tv_day_ro);
            holder.tvWeekRo = (TextView)rowView.findViewById(R.id.tv_week_ro);
            holder.ivVerticalLine = (View)rowView.findViewById(R.id.iv_vertical_line);
            holder.detailLayout = (LinearLayout)rowView.findViewById(R.id.llro_detail);
            rowView.setTag(holder);
            viewMap.put(position, rowView);
        }
        else
        {
            holder = (ViewHolder)rowView.getTag();
        }
        
        //final LinearLayout finaldetailLayout =;
        holder.tvDateRo.setText(dm.getCurrYear() + "-" + dm.getCurrMonth());
        holder.tvDayRo.setText(dm.getCurrDay() + "");
        holder.tvWeekRo.setText(dayNames[dm.getCurrWeek() - 1]);
        //如果是当天，把颜色设为高亮
        Date toDay = new Date();
        if (dm.getCurrDay() == toDay.getDate() && dm.getCurrMonth() == toDay.getMonth()+1)
        {
            holder.tvDayRo.setTextColor(context.getResources().getColor(R.color.orange));
        }
        //--------------item2
        DateModel dateModel = dateList.get(position);
        String strdm = dateModel.toString();
        //LogUtil.d(strdm);
        if (planList.size() > 0)
        {
            //LinkedHashMap<String, View> sunViewMap = (LinkedHashMap<String, View>)holder.detailLayout.getTag();
            LinkedHashMap<String, View> sunViewMap = itemViewMap.get(position);
            if (sunViewMap == null)
            {
                sunViewMap = new LinkedHashMap<String, View>();
            }
            holder.detailLayout.removeAllViews();
            for (Plan plan : planList)
            {
                //LogUtil.d("remoteId="+plan.getsRemoteId());
                //LogUtil.d(plan.getAlarmTime());
                if (plan.getAlarmTime().contains(strdm))
                {
                    //LogUtil.d("日期包含" + "");
                    View itemView = sunViewMap.get(plan.getTitle());
                    if (itemView == null)
                    {
                        itemView = mInflater.inflate(R.layout.plan_list_item_sun,
                                null);
                        holder.tvListTitleRo = (TextView)itemView.findViewById(R.id.tv_list_title_ro);
                        holder.tvListTimeRo = (TextView)itemView.findViewById(R.id.tv_list_time_ro);
                        holder.ivIsFinish = (ImageView)itemView.findViewById(R.id.iv_is_finish);
                        holder.tvIsFinish = (TextView)itemView.findViewById(R.id.tv_is_finish);
                        //holder.layButtons = (LinearLayout)itemView.findViewById(R.id.lay_buttons);
                        //设置布局属性
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT);
                        itemView.setLayoutParams(params);
                        
                        holder.tvListTimeRo.setText(plan.getDateTime());
                        holder.tvListTitleRo.setText(plan.getTitle());
                        
                        
                    }
                    Date today = null;
                    try
                    {
                        today = DateUtil.getNowDate();
                    }
                    catch (ParseException e)
                    {
                        
                    }
                    holder.tvIsFinish = (TextView)itemView.findViewById(R.id.tv_is_finish);
                    if (plan.getFinish() == Plan.FINISH_YES)
                    {
                        //holder.ivIsFinish.setImageResource(R.drawable.answer);
                        holder.tvIsFinish.setTextColor(context.getResources().getColor(R.color.gray));
                        holder.tvIsFinish.setText("^_^完成了");
                    }
                    //未完成且过期
                    else if (plan.getFinish() == Plan.FINISH_NO
                            && today.compareTo(DateUtil.strToDate(plan.getAlarmTime())) > 0)
                    {
                        //holder.ivIsFinish.setImageResource(R.drawable.answer);
                        holder.tvIsFinish.setText("还没完成哦");
                    }
                    //正在进行
                    else
                    {
                        //holder.ivIsFinish.setImageResource(R.drawable.answer);
                        holder.tvIsFinish.setTextColor(context.getResources().getColor(R.color.blue));
                        holder.tvIsFinish.setText("计划中");
                    }
                    
                    itemView.setTag(plan.getpLocalId());
                    itemView.setOnClickListener(new OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            subItemClickListener.onClick(v,position);
                        }
                    });
                    sunViewMap.put(plan.getTitle(), itemView);
                    
                    holder.detailLayout.addView(itemView);
                }
            }
            //如果是该日期最后一个计划，则将下划线隐藏
            int size = holder.detailLayout.getChildCount();
            if(size >0)
            {
                
                View view1 = holder.detailLayout.getChildAt(size-1);
                if(view1 != null)
                {
                    view1.findViewById(R.id.view_line).setVisibility(View.GONE);
                }
                
            }
            //holder.detailLayout.setTag(sunViewMap);
            itemViewMap.put(position, sunViewMap);
        }
        //计算高度
        int height = rowView.getMeasuredHeight();
        //LogUtil.d("高度："+height);
        if(height>8)
        {
            LayoutParams layoutParams = holder.ivVerticalLine.getLayoutParams();
            layoutParams.height = height-8;
            holder.ivVerticalLine.setLayoutParams(layoutParams);
        }
        
        return rowView;
    }
    public class ViewHolder
    {
        public TextView tvDateRo;
        public TextView tvDayRo;
        public TextView tvWeekRo;
        public View ivVerticalLine;//竖线
        public LinearLayout detailLayout;
        public TextView tvListTitleRo;
        public TextView tvListTimeRo;
        public ImageView ivIsFinish;//是否完成标志，暂时不用，用文字代替
        public TextView tvIsFinish;//是否完成
        //public LinearLayout layButtons;//删除/完成按钮
    }
    
    public interface OnSubItemClickListener
    {
        public void onClick(View v,int index);
    }
    
    public void setOnSubItemClickListener(OnSubItemClickListener listener)
    {
        subItemClickListener = listener;
    }

    @Override
    public void notifyDataSetChanged()
    {
        viewMap.clear();
        itemViewMap.clear();
        super.notifyDataSetChanged();
    }

    public void setPlanList(List<Plan> planList)
    {
        this.planList = planList;
    }
    
    
}
